package common.自定义返回值类型处理器;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 日期时间消息转换器
 * 数据库是时间日期的话,后台直接用String来接收,这样的话可以自动转好
 * 案例:ZJJ_Mybatis_2020/05/18_14:29:57_vujdu
 *
 * @author zjj
 */
@MappedJdbcTypes({JdbcType.TIMESTAMP})
@MappedTypes({String.class})
public class CustomTypeHandler extends BaseTypeHandler<String> {
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setString(i, parameter);
	}

	@Override
	public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return substring(rs.getString(columnName));
	}

	@Override
	public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return rs.getString(columnIndex);
	}

	@Override
	public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		return cs.getString(columnIndex);
	}

	private String substring(String value) {
		if (!"".endsWith(value)) {
			return value.substring(0, value.length() - 2);
		}
		return value;
	}
}
